package com.iteaj.iot.client.mqtt.impl;

import com.iteaj.iot.AbstractProtocol;
import com.iteaj.iot.ProtocolFactory;
import com.iteaj.iot.client.mqtt.MqttConnectProperties;
import com.iteaj.iot.client.mqtt.message.MqttMessageHead;
import io.netty.handler.codec.mqtt.MqttPublishMessage;
import io.netty.handler.codec.mqtt.MqttTopicSubscription;

import java.util.List;

/**
 * Mqtt客户端默认实现需要的钩子对象
 * @see MqttDefaultClientMessage
 * @see MqttDefaultClientComponent#MqttDefaultClientComponent(MqttDefaultImplProcessor)
 */
public interface MqttDefaultImplProcessor {

    /**
     * 此客户端要订阅的主题
     * @see MqttDefaultClientComponent#doSubscribe(MqttConnectProperties)
     * @param client
     * @return 当前客户端需要订阅的Topic
     */
    List<MqttTopicSubscription> subscribe(MqttConnectProperties client);

    /**
     * 解析报文头
     * @see MqttDefaultClientMessage#doBuild(byte[])
     * @param payload
     * @param message
     * @return {@link MqttMessageHead#getEquipCode()} {@link MqttMessageHead#getMessageId()} {@link MqttMessageHead#getType()}
     */
    MqttMessageHead buildHeader(byte[] payload, MqttDefaultClientMessage message);

    /**
     * 获取指定的协议
     * @see MqttDefaultClientComponent#getProtocol(MqttDefaultClientMessage)
     * @param message
     * @param factory
     * @return
     */
    AbstractProtocol getProtocol(MqttDefaultClientMessage message, ProtocolFactory factory);

    /**
     * 创建默认Mqtt报文
     * @see com.iteaj.iot.client.mqtt.MqttClient#buildPublishMessage(MqttPublishMessage)
     * @param message
     * @return
     */
    default MqttDefaultClientMessage newInstance(MqttPublishMessage message) {
        return new MqttDefaultClientMessage(message, this);
    }
}
